package com.example.jt_manager_web;

import com.example.jt_common.EasyUIResult;
import com.example.jt_common.SysResult;
import com.example.jt_manager_pojo.Content;
import com.example.jt_manager_pojo.ContentCategory;
import com.example.jt_manager_service.ContentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {
    @Autowired
    ContentService contentService;

    @RequestMapping("category/list")
    @ResponseBody
    public List<ContentCategory> categoryList(@RequestParam(name = "id",required = false,defaultValue = "0") long id){
        return contentService.queryCatById(id);
    }

    @RequestMapping("category/create")
    @ResponseBody
    public SysResult categoryAdd(long parentId,String name){
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setName(name);
        contentCategory.setCreated(new Date());
        contentCategory.setUpdated(new Date());
        contentCategory.setParentId(parentId);
        contentService.addCategory(contentCategory,parentId);
        return SysResult.ok(contentCategory);
    }

    @RequestMapping("category/delete")
    @ResponseBody
    public SysResult categoryDel(long parentId,long id){
        contentService.delCategory(id,parentId);
        return SysResult.ok();
    }

    @RequestMapping("category/update")
    @ResponseBody
    public SysResult categoryUpdate(long id,String name){
        contentService.updateNameById(id,name);
        return SysResult.ok();
    }

    @RequestMapping("query/list")
    @ResponseBody
    public EasyUIResult queryList(long categoryId,int page,int rows){
        PageInfo<Content> pageInfo = contentService.queryList(categoryId,page,rows);
        EasyUIResult easyUIResult = new EasyUIResult();
        easyUIResult.setRows(pageInfo.getList());
        easyUIResult.setTotal(pageInfo.getTotal());
        return easyUIResult;
    }

    @RequestMapping("edit")
    @ResponseBody
    public SysResult contentEdit(Content content){
        content.setUpdated(new Date());
        contentService.updateContent(content);
        return SysResult.ok();
    }

    @RequestMapping("delete")
    @ResponseBody
    public SysResult contentDelete(long[] ids){
        List<Long> list = new ArrayList<>();
        for (long id :
                ids) {
            list.add(id);
        }
        contentService.deleteContentsByIds(list);
        return SysResult.ok();
    }

    @RequestMapping("save")
    @ResponseBody
    public SysResult contentSave(Content content){
        content.setCreated(new Date());
        content.setUpdated(new Date());
        contentService.insertContent(content);
        return SysResult.ok();
    }
}
